package com.abe.libcore.utils

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import android.os.Bundle
import android.text.TextUtils

object CommonUtils {

    //日志打印
    fun log(message: String?) {
        println(message)
    }

    //获取Application的Meta数据
    fun <T> getMetaData(context: Context?, key: String?, defaultValue: T?): Any? {
        if (context == null || TextUtils.isEmpty(key)) {
            log("context == null:" + (context == null) + "||key == null:" + TextUtils.isEmpty(key))
            return null
        }
        val bundle: Bundle = getMetaDataBundle(context, key) ?: return null
        return bundle.getString(key) ?: defaultValue
    }

    //获取Application的Meta Bundle数据
    fun getMetaDataBundle(context: Context?, key: String?): Bundle? {
        if (context == null || TextUtils.isEmpty(key)) {
            log("context == null:" + (context == null) + "||key == null:" + TextUtils.isEmpty(key))
            return null
        }
        val app: ApplicationInfo = context.packageManager.getApplicationInfo(
            context.packageName,
            PackageManager.GET_META_DATA
        )
        return app.metaData
    }
}